var seconds = 120;
   const IPaddr = "http://10.141.31.239/Usersmanage";      //服务器IP
//const IPaddr = "http://10.139.23.100:8080/Usersmanage";   //ZFC IP
var token = $.cookie("access_token");

layui.use('element', function () {
    var element = layui.element;
});
var verifyCode = new GVerify("authCodeImg");

var g_passsword_salt = "1a2b3c4d123";      //md5加密盐值

layui.use('element', function () {
    var element = layui.element;
});
var verifyCode = new GVerify("authCodeImg");
var ajaxMail;
var ajaxPhone;

$(document).ready(function () {
    $.ajax({
        type: "post",
        url: IPaddr + "/modify/userInfo",
        async: true,
        headers: {"Authorization": token,},
        success: function(result){
            var classify = window.location.href.split('updatePassword')[1].split('.html')[0];
            if(classify == "ByMail"){
                document.getElementById('emailAddr').innerHTML = result.data.email;
                ajaxMail = result.data.email;
            }else if(classify == "ByPhone"){
                document.getElementById('phoneAddr').innerHTML = result.data.telephone;
                ajaxPhone = result.data.telephone;
            }else {
                ajaxPhone = result.data.telephone;
                ajaxMail = result.data.email;
            }
            // console.log(classify);
            // document.getElementById('email').innerHTML = result.data.email;
            // document.getElementById('phoneNum').innerHTML = result.data.telephone;
            // document.getElementById('bankCardNum').innerHTML = result.data.bankCarbum;
            // document.getElementById('realName').innerHTML = result.data.identityName;
            // document.getElementById('IDNum').innerHTML = result.data.identityCardNum;
        },
        error: function (e) {
            // console.log(e);
        },
    });
});

/*updatePasswordByMail.html内函数  开始*/

function sendMailCode() {
    $.ajax({
        // url: IPaddr + "/user/sendVerificationCodeByEmail",
        url: IPaddr + "/user/sendVerificationCodeByEmail",
        type: "POST",
        data: {"email": ajaxMail},
        headers:{"Authorization": token},
        success: function (data) {
            if(data.code == 0){
                document.getElementById('mailText').innerHTML = '邮件已发送，如果没收到邮件，请检查您的垃圾箱或广告箱';
                document.getElementById('mailText').style.display = 'block';

                document.getElementById('mailCode').disabled = '';

                document.getElementById('sendMailCode').disabled = "disabled";
                document.getElementById('sendMailCode').onclick = "";
                document.getElementById('sendMailCode').style.opacity = "0.2";
                document.getElementById('sendMailCode').style.cursor = "default";
            }else {
                document.getElementById('mailText').innerHTML = '邮件未发送成功，请稍后再试';
                document.getElementById('mailText').style.display = 'block';
            }
        },
        error: function (e) {
            document.getElementById('mailText').innerHTML = '邮件未发送成功，请稍后再试';
            document.getElementById('mailText').style.display = 'block';
        }
    });
}

/*验证码是否输入正确*/
function authCodeCheck() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    let mailCode = document.getElementById('mailCode').value;
    if(res){
        $.ajax({
            url: IPaddr + "/user/verifyVerificationCodeByEmail",
            type: "POST",
            data:{"verificationCode": mailCode, "email": ajaxMail},
            headers:{"Authorization": token},
            success: function (data) {
                console.log(data);
                if(data.code == 0)
                    self.location.href = "updatePasswordStepTwo.html";
                else {
                    document.getElementById('mailError').innerHTML = '邮箱验证码有误，请再次确认';
                    document.getElementById('mailError').style.display = 'block';
                    document.getElementById('mailText').style.display = 'none';
                }
            },
            error: function (e) {
                console.log(e);
            }
        });
    }else{
        document.getElementById('authCodeError').innerHTML = '验证码错误';
        document.getElementById('authCodeError').style.display = 'block';
        document.getElementById("authCode").value = '';
    }
}

/*邮箱验证码input得到焦点时*/
function mailCodeOnfocus() {
    document.getElementById('mailError').style.display = 'none';
}
/*updatePasswordByMail.html内函数  结束*/


/*updatePasswordStepTwo.html内函数  开始*/

/*检查修改登陆的两次结果是否一致和验证码是否正确*/
function checkPassword() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    if(confirmPasswordOnblur() && passwordOnblur()){
        // console.log(confirmPasswordOnblur());
        if(res){
            var oldInputPassword = document.getElementById('oldPassword').value;
            var inputPassword = document.getElementById('confirmPassword').value;
            var strpass1 = ""+g_passsword_salt.charAt(2)+g_passsword_salt.charAt(3)+g_passsword_salt.charAt(1)+oldInputPassword+g_passsword_salt.charAt(6)+g_passsword_salt.charAt(7);
            var strpass2 = ""+g_passsword_salt.charAt(2)+g_passsword_salt.charAt(3)+g_passsword_salt.charAt(1)+inputPassword+g_passsword_salt.charAt(6)+g_passsword_salt.charAt(7);
            var oldPassword = $.md5(strpass1);
            var newPassword = $.md5(strpass2);
            document.getElementById('authCodeError').style.display = 'none';
            /*发送ajax请求,接口在开发中*/
            $.ajax({
                url: IPaddr + '/modify/modifyPassword',
                type: 'POST',
                data: {"oldPassword": oldPassword,"newPassword": newPassword},
                headers:{"Authorization": token},
                success: function (data) {
                    console.log(data);
                    if(data.code == 0)
                        self.location.href = 'updatePasswordSuccess.html';
                    else if(data.code == 500625){
                        document.getElementById('authCodeError').innerHTML = '新旧密码重复';
                        document.getElementById('authCodeError').style.display = 'block';
                    }else {
                        document.getElementById('authCodeError').innerHTML = '修改密码失败，请稍后再试';
                        document.getElementById('authCodeError').style.display = 'block';
                    }
                },
                error: function (e) {
                    // console.log(e);
                    document.getElementById('authCodeError').innerHTML = '修改密码失败，请稍后再试';
                    document.getElementById('authCodeError').style.display = 'block';
                }
            })
        }else {
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
            document.getElementById("authCode").value = '';
        }
    }else {
    }
}
/*密码输入提示*/
function passwordOnfocus() {
    document.getElementById('passwordMessage').style.display = 'block';
    document.getElementById('passwordError').style.display = 'none';
}
/*检查两次密码是否一致*/
function confirmPasswordOnblur() {
    let password = document.getElementById('password').value;
    let confirmPassword = document.getElementById('confirmPassword').value;
    if(password != confirmPassword){
        document.getElementById('confirmPasswordError').style.display = 'block';
        return false;
    }else {
        document.getElementById('confirmPasswordError').style.display = 'none';
        return true;
    }
}
function confirmPasswordFocus() {
    document.getElementById('confirmPasswordError').style.display = 'none';
}
/*验证码input得到焦点时*/
function authCodeFocus() {
    document.getElementById('authCodeError').style.display = 'none';
}
/*updatePasswordStepTwo.html内函数  结束*/



/*updatePasswordByPhone.html内函数  开始*/

/*检查手机校验码的长的和是否输入了*/
function messageCodeOnblur() {
    let val = document.getElementById('messageCode').value;
    if(val == ''|| val == null){                    //未输入手机校验码
        document.getElementById('textError').innerHTML = '请输入手机校验码';
        document.getElementById('textError').style.display = 'block';
        return false;
    }else if(val.length != 6){       //手机校验码的长度为6
        document.getElementById('textError').innerHTML = '请输入正确长度的手机校验码';
        document.getElementById('textError').style.display = 'block';
        return false;
    }else {
        return true;
    }
}

function messageCodeOnfocus() {
    document.getElementById('textError').style.display = 'none';
}
/*手机校验码和验证码检查*/
function phoneCheck() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());

    if(document.getElementById('messageCode').value.length == 0){
        document.getElementById('textError').innerHTML = '请输入手机校验码';
        document.getElementById('textError').style.display = 'block';
    }else if(document.getElementById('messageCode').value.length != 6){
        document.getElementById('textError').innerHTML = '请输入正确长度的手机校验码';
        document.getElementById('textError').style.display = 'block';
    }else {
        if(res){
            // alert("验证正确");
            document.getElementById('authCodeError').style.display = 'none';
            self.location.href = "";
        }else{
            document.getElementById("authCode").value = '';
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
        }
    }
}

/*点击获取短信校验码时，让链接不可点击并变暗*/
function sendCode() {
    document.getElementById('messageText').style.display = 'block';
    document.getElementById('messageCode').disabled = '';

    document.getElementById('sendMessage').disabled = "disabled";
    document.getElementById('sendMessage').onclick = "";
    document.getElementById('sendMessage').style.opacity = "0.2";
    document.getElementById('sendMessage').style.cursor = "default";

    counterMinus();
}

/*倒计时120秒后“获取短信校验码”恢复可点击状态*/
function counterMinus() {
    if(seconds == 0){
        document.getElementById('messageText').style.display = 'none';
        document.getElementById('sendMessage').disabled = "";
        document.getElementById('sendMessage').setAttribute("onclick", "sendCode()");
        document.getElementById('sendMessage').style.opacity = "1";
        document.getElementById('sendMessage').style.cursor = "pointer";
        seconds = 120;
        // document.getElementById('sendMessage').onclick = "";
        return;
    }else {
        document.getElementById('counter').innerHTML = seconds;
        seconds--;
        setTimeout("counterMinus()", 1000);
    }
}
/*updatePasswordByPhone.html内函数  结束*/





/*检查email地址是否合法*/
function checkMailAddr() {
    document.getElementById('emailText').style.display = 'none';
    let rightMailAddr = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
    let mailAddr = document.getElementById('emailAddr').value;
    if(rightMailAddr.test(mailAddr)){
        // alert(1);
        // document.getElementById('emailError').style.display = 'block';
    }else {
        document.getElementById('emailError').style.display = 'block';
    }
}

function mailAddrOnfocus() {
    document.getElementById('emailText').style.display = 'block';
    document.getElementById('emailError').style.display = 'none';
}

/*提交新邮箱时，检查邮箱和验证码是否正确*/
function mailCheck() {
    /*验证验证码是否正确*/
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());

    /*验证邮箱格式是否正确*/
    let rightMailAddr = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
    let mailAddr = document.getElementById('emailAddr').value;
    let mailRight = rightMailAddr.test(mailAddr);

    if(mailRight){
        if(res){
            document.getElementById('authCodeError').style.display = 'none';
            document.getElementById('authCodeError').innerHTML = '';
            self.location.href = "";
        }else {
            document.getElementById('authCodeError').style.display = 'block';
            document.getElementById('authCodeError').innerHTML = '验证码不正确';
            // console.log('error');
        }
    }else {
        document.getElementById('emailError').style.display = 'block';
        if(res){
            document.getElementById('authCodeError').style.display = 'none';
            document.getElementById('authCodeError').innerHTML = '';
        }else {
            document.getElementById('authCodeError').style.display = 'block';
            document.getElementById('authCodeError').innerHTML = '验证码不正确';
        }
    }
}


// /*验证码是否输入正确*/
// function authCodeCheck() {
//     let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
//     // let currentUrl = window.location.href.split("update")[1].split("By")[0];   //截取URL判断是更新邮箱，登陆密码还是手机号码
//     if(res){
//         // console.log("send");
//         $.ajax({
//             url: IPaddr + "/email/sentVerificationMail",
//             type: "GET",
//             data:{"email": "353226475@qq.com", "name": "zhouchang"},
//             success: function (data) {
//                 console.log(data);
//                 if(data.code == 0){}
//             },
//             error: function (e) {
//                 console.log(e);
//             }
//         });
//         // self.location.href = "update" + currentUrl +"MailSend.html";
//     }else{
//         document.getElementById('authCodeError').innerHTML = '验证码错误';
//         document.getElementById('authCodeError').style.display = 'block';
//         document.getElementById("authCode").value = '';
//     }
// }

function passwordOnblur() {
    document.getElementById('passwordMessage').style.display = 'none';
    let val = document.getElementById('password').value;
    if(val.length < 6){
        document.getElementById('passwordError').innerHTML = '密码长度不正确，请重新输入';
        document.getElementById('passwordError').style.display = 'block';
        return false;
    }else {
        let modes = 0;
        if (/\d/.test(val)) modes++; //数字
        if (/[a-z]/.test(val)) modes++; //小写
        if (/[A-Z]/.test(val)) modes++; //大写
        if (/\W/.test(val)) modes++; //特殊字符

        if(modes < 3){
            document.getElementById('passwordError').innerHTML = '密码强度弱，请重新输入';
            document.getElementById('passwordError').style.display = 'block';
            return false;
        }else
            return true;
    }
    // console.log(val);
}


/**/
function phoneNumFocus() {
    document.getElementById('messageCodeError').style.display = 'none';
}

/*检查电话号码的长度*/
function phoneNumOnblur() {
    if(document.getElementById('phoneNum').value.length != 11) {
        document.getElementById('messageCodeError').innerHTML = '电话号码长度不正确';
        document.getElementById('messageCodeError').style.display = 'block';
        return false;
    }else {
        document.getElementById('messageCodeError').style.display = 'none';
        return true;
    }
}


/*检查修改手机号码时新手机号码和验证码是否正确*/
function checkPhoneNum() {
    let res = verifyCode.validate(document.getElementById("authCode").value.toLocaleLowerCase());
    if(phoneNumOnblur() && messageCodeOnblur()){
        if(res){
            document.getElementById('authCodeError').style.display = 'none';
            console.log(123);
        }else {
            document.getElementById("authCode").value = '';
            document.getElementById('authCodeError').innerHTML = '验证码错误';
            document.getElementById('authCodeError').style.display = 'block';
        }
    }
}
